package me.wangqu.weiyouqu.web.rest;

import com.codahale.metrics.annotation.Timed;
import me.wangqu.weiyouqu.security.SecurityUtils;
import me.wangqu.weiyouqu.web.rest.business.model.WithdrawAccount;
import me.wangqu.weiyouqu.web.rest.business.model.WithdrawModel;
import me.wangqu.weiyouqu.web.rest.business.service.WithDrawService;
import me.wangqu.weiyouqu.web.rest.util.HeaderUtil;
import me.wangqu.weiyouqu.web.rest.util.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.util.List;

/**
 * REST controller for managing Withdraw.
 */
@RestController
@RequestMapping("/api")
public class WithdrawResource {

    private final Logger log = LoggerFactory.getLogger(WithdrawResource.class);
    @Autowired
    private WithDrawService withDrawService;

    /**
     * 查询提现列表
     *
     * @param pageable
     * @return
     * @throws URISyntaxException
     */
    @RequestMapping(value = "/withdraws/self",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Transactional(readOnly = true)
    public ResponseEntity<List<WithdrawModel>> getSelfWithdraws(Pageable pageable)
        throws URISyntaxException {
        log.debug("查询提现列表");
        return new ResponseEntity<>(withDrawService.getSelfWithdraws(SecurityUtils.getCurrentUserLogin(), pageable), HttpStatus.OK);
    }

    /**
     * 获取帐户余额及支付宝信息(微信提现后期实现) - 用于提现流程
     *
     * @return
     */
    @RequestMapping(value = "/withdraws/self-account",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<WithdrawAccount> getSelfCashAccount() {
        log.debug("获取帐户信息");
        return new ResponseEntity<>(withDrawService.getSelfCashAccount(SecurityUtils.getCurrentUserLogin()), HttpStatus.OK);
    }

    /**
     * 设置账户信息(微信提现后期实现) - 用于提现流程
     *
     * @param model
     * @return
     * @throws URISyntaxException
     */
    @RequestMapping(value = "/withdraws/self-account",
        method = RequestMethod.POST,
        produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE})
    @Timed
    public ResponseEntity<?> postSelfCashAccount(@RequestBody WithdrawModel model) throws URISyntaxException {
        log.debug("设置账户信息 : {}", model);
        if (model.getType() == Constant.ACCOUNT_TYPE_ZHIFUBAO && !StringUtils.isEmpty(model.getAccount()) && !StringUtils.isEmpty(model.getRealName())) {
            String login = SecurityUtils.getCurrentUserLogin();
            if (withDrawService.isSelfRealName(login)) {
                return new ResponseEntity<String>("支付宝账户已设置, 无法修改", HttpStatus.BAD_REQUEST);
            } else {
                log.info("设置支付宝账户");
                if (withDrawService.setSelfCashAccount(model, login)) {
                    return new ResponseEntity<String>("支付宝账户设置成功", HttpStatus.OK);
                }


            }
        }
        return new ResponseEntity<String>("帐户信息设置错误", HttpStatus.BAD_REQUEST);
    }

    /**
     * POST  /withdraws/self : 提现操作
     *
     * @param model
     * @return
     * @throws URISyntaxException
     */
    @RequestMapping(value = "/withdraws/self",
        method = RequestMethod.POST,
        produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE})
    @Timed
    public ResponseEntity<?> postSelfWithdraw(@RequestBody WithdrawModel model) throws URISyntaxException {
        log.debug("提现操作: ", model);
        if (model.getAmount() == null || model.getType() == null) {
            return ResponseEntity.badRequest().body("参数错误");
        }
        if (BigDecimal.valueOf(20).compareTo(model.getAmount()) > 0 || model.getAmount().doubleValue() % 10 != 0) {
            return ResponseEntity.badRequest().body("提现金额必须大于 20 , 且是 10 的倍数");
        }
        if (model.getType() == Constant.ACCOUNT_TYPE_ZHIFUBAO) {
            // 支付宝提现操作;
            if (withDrawService.postSelfWithdraw(model, SecurityUtils.getCurrentUserLogin())) {
                return ResponseEntity.ok().body(null);
            } else {
                return ResponseEntity.badRequest().body("提现失败");
            }
        } else {
            return ResponseEntity.badRequest().body("暂不支持该类型提现");
        }
    }

}
